home *** CD-ROM | disk | FTP | other *** search
/ NetNews Offline 2 / NetNews Offline Volume 2.iso / news / comp / lang / c-part1 / 7684 < prev    next >
Encoding:
Text File  |  1996-08-05  |  3.9 KB  |  76 lines

  1. Path: keats.ugrad.cs.ubc.ca!not-for-mail
  2. From: c2a192@ugrad.cs.ubc.ca (Kazimir Kylheku)
  3. Newsgroups: comp.lang.c
  4. Subject: Re: Processing a structure- how?
  5. Date: 26 Feb 1996 13:19:23 -0800
  6. Organization: Computer Science, University of B.C., Vancouver, B.C., Canada
  7. Message-ID: <4gt84rINN21p@keats.ugrad.cs.ubc.ca>
  8. References: <DnDv98.Ko1@network.com>
  9. NNTP-Posting-Host: keats.ugrad.cs.ubc.ca
  10.  
  11. In article <DnDv98.Ko1@network.com>,
  12. Mike Collins <collim@anubis.network.com> wrote:
  13. >Folks,
  14. >
  15. >I want to write a generic routine that will print data contained 
  16. >in structures on the screen (of a DOS-based PC, but I don't think that 
  17. >comes into it). What I want is to be able to pass a structure  or 
  18. >structure pointer to the routine, which will then be able to find out what 
  19. >the contents of the structure are, so that if I pass a customer structure 
  20. >containing surname, forename, address, phone number, I will be able to 
  21. >print this. If I pass a different structure containing catalog data, I 
  22. >will be able to print Item, category, bin location, etc.
  23. >
  24. >Is there a way for a function to find out how many fields are in a 
  25. >structure and what kind of variable each is (int, char[], etc)? I reallise 
  26. >that this information is known to the compiler and not to the compiled 
  27. >program as such, but can the compiler be made to embed information of this 
  28. >kind into a program?
  29.  
  30. Yes and no. You need to develop your own system of augmenting a structure
  31. declaration with additional info that can be used by special routines.
  32.  
  33. One way is to invent a data definition language, and write a data definition
  34. compiler. You write your data object specifications in this language, and the
  35. definition compiler will generate C language headers containing the requisite
  36. struct declarations, and will perhaps declare some sort of meta-information
  37. structures that will tell certain library routines extra information about the
  38. structure fields. The compiler can even automatically generate C code for
  39. filter routines for performing certain structure-specific operations on the
  40. data (such as file input or output).
  41.  
  42. >The effect would be like a printf() statement (I don't mind sending it a 
  43. >formatting string), but with only a structure or structure pointer as a 
  44. >data argument.
  45.  
  46. An automatically generated filter routine would do this for you. The
  47. challenging part is writing the processor. A robust data definition compiler
  48. would parse a language that allows nested structure definitions.
  49.  
  50. I have worked with a number of such programs, designed for various purposes.
  51. Sun's ``rpcgen'' interface compiler will generate C header files and
  52. client/server skeleton/stub code from structure and function definitions. You
  53. fill in the blanks and have a client/server system working. (SunSoft's latest
  54. project, the Spring operating system, has a new interface compiler for
  55. language-independent OO client/server programming). The rpcgen compiler
  56. generates filter routines, for each structure, for serializing it into an
  57. external, machine-independent representation, and for deserializing back from
  58. that representation. A recent project of mine relies heavily on this function
  59. of rpcgen---we have ported the XDR library to Windows/Winsock for the purpose
  60. to support client software on that platform.
  61.  
  62. I've also worked with some database toolkits with such generators; for example
  63. the Typhoon library, which  uses the data definition compiler to generate a
  64. description of records, fields and their relationships which the database
  65. engine understands. The database engine can then work with the resulting raw C
  66. structures, which are directly written to the indexed database files. It's
  67. fast, but I would not recommend this product except as an example of what you
  68. can do with a little automatically-generated C.
  69.  
  70. There is probably freely available software that will do what you are looking
  71. for, by the way, so unless you are skilled in the use of compiler construction
  72. tools and can whip this thing out in less than a day, I'd do a little browsing
  73. first.
  74. -- 
  75.  
  76.